package study.springcloud.discovery.consul.support.consul;

import com.ecwid.consul.v1.ConsulClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
import org.springframework.cloud.consul.discovery.TtlScheduler;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistrationCustomizer;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;

@Slf4j
//@Configuration
@AutoConfigureBefore(ConsulServiceRegistryAutoConfiguration.class)
public class MyConsulServiceRegistryAutoConfiguration {

    @Bean
    public ConsulServiceRegistry consulServiceRegistry(ConfigurableEnvironment environment,
                                                       ConsulClient client,
                                                       ConsulDiscoveryProperties properties,
                                                       @Autowired(required = false) TtlScheduler ttlScheduler,
                                                       HeartbeatProperties heartbeatProperties) {

        return new MyConsulServiceRegistry(environment, client, properties, ttlScheduler, heartbeatProperties);
    }

    @Bean
    public ConsulRegistrationCustomizer consulRegistrationCustomizer(ConfigurableEnvironment environment) {
        return new MyConsulRegistrationCustomizer(environment);
    }
}
